/**
 * Twitter OAuth
 * @author muik
 */
 
function TwitterOAuth() {
}
TwitterOAuth.prototype = {
	isAuthed: function() {
		try {
			return options.getValue('user_id').length > 0;
		} catch (e) {
			return false;
		}
	},
	__get_params: function(message, accessor) {
		OAuth.setTimestampAndNonce(message);
		OAuth.SignatureMethod.sign(message, accessor);
		var parameterMap = OAuth.getParameterMap(message.parameters);
		var params = [];
		for (var p in parameterMap) {
			if (p.substring(0, 6) == "oauth_") {
				params.push(p + '=' + encodeURIComponent(parameterMap[p]));
			}
		}
		return params.join('&');
	},
	requestToken: function(onComplete) {
		var accessor = { consumerSecret: consumer.consumerSecret
					   , tokenSecret   : ''};
		var message = { action: consumer.serviceProvider.requestTokenURL
				  , method: 'POST'
				  , parameters: []
				  };
		message.parameters.push(['oauth_consumer_key', consumer.consumerKey]);
		message.parameters.push(['oauth_signature_method', consumer.serviceProvider.signatureMethod]);
		
		var req = new XMLHttpRequest();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status != 200) {
					alert('ERROR: fail to request token');
					return false;
				}
				
				var match = req.responseText.match(/oauth_token=([^&]+)&oauth_token_secret=([^&]+)/);
				options.putValue('oauth_token', match[1]);
				options.putValue('oauth_token_secret', match[2]);
				onComplete(req.responseText);
			}
		};
		req.open('POST', message.action, true);
		req.send(this.__get_params(message, accessor));
	},
	requestAccessToken: function(pinCode, onComplete) {
		var accessor = { consumerSecret: consumer.consumerSecret
					   , tokenSecret   : options.getValue('oauth_token_secret') };
		var message = { action: consumer.serviceProvider.accessTokenURL
				  , method: 'POST'
				  , parameters: []
				  };
		message.parameters.push(['oauth_consumer_key', consumer.consumerKey]);
		message.parameters.push(['oauth_signature_method', consumer.serviceProvider.signatureMethod]);
		message.parameters.push(['oauth_token', options.getValue('oauth_token')]);
		message.parameters.push(['oauth_verifier', pinCode]);
		
		var req = new XMLHttpRequest();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status != 200) {
					alert('ERROR: fail to request access token');
					return false;
				}
				
				var params = req.responseText.split('&');
				var data = {};
				for (var i = 0; i < params.length; i++) {
					var match = params[i].match(/([^=]+)=(.+)/);
					data[match[1]] = match[2];
				}
				options.putValue('oauth_token', data.oauth_token);
				options.putValue('oauth_token_secret', data.oauth_token_secret);
				options.putValue('user_id', data.user_id);
				options.putValue('screen_name', data.screen_name);
				onComplete();
			}
		};
		req.open('POST', message.action, true);
		req.send(this.__get_params(message, accessor));
	}
};